<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        textarea, input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        button:hover {
            background: #0056b3;
        }
        button:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
            word-break: break-word;
        }
        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .loading {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>子系统P API测试</h1>
        
        <div class="form-group">
            <label for="apiUrl">API地址:</label>
            <input type="text" id="apiUrl" value="https://test.yxyd.com/api/ask" readonly>
        </div>
        
        <div class="form-group">
            <label for="question">问题:</label>
            <textarea id="question" placeholder="请输入您的问题...">我想了解一下育儿方面的知识，请给我一些建议。</textarea>
        </div>
        
        <div class="form-group">
            <label for="expertName">专家名称:</label>
            <input type="text" id="expertName" value="张博士" readonly>
        </div>
        
        <button onclick="testAPI()">测试API调用</button>
        
        <div id="result" class="result" style="display: none;"></div>
    </div>

    <script>
        async function testAPI() {
            const button = document.querySelector('button');
            const result = document.getElementById('result');
            const question = document.getElementById('question').value.trim();
            
            if (!question) {
                alert('请输入问题');
                return;
            }
            
            // 显示加载状态
            button.disabled = true;
            button.textContent = '正在调用API...';
            result.style.display = 'block';
            result.className = 'result loading';
            result.textContent = '正在向子系统P发送请求...';
            
            try {
                const requestData = {
                    question: question,
                    expert: {
                        id: 1,
                        nickname: '张博士',
                        bio: '儿科专家，从事儿童健康和发育研究20年',
                        domain: '育儿教育',
                        classic_cases: []
                    },
                    preconditions: {
                        child_age: '1-3岁',
                        child_gender: '男孩'
                    },
                    domain_info: {
                        name: '育儿教育',
                        description: '专业的育儿知识和早期教育指导',
                        welcome_message: '欢迎来到育儿教育咨询！'
                    }
                };
                
                console.log('发送请求数据:', requestData);
                
                const response = await fetch('https://test.yxyd.com/api/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(requestData)
                });
                
                console.log('响应状态:', response.status);
                console.log('响应头:', response.headers);
                
                const responseText = await response.text();
                console.log('响应内容:', responseText);
                
                let responseData;
                try {
                    responseData = JSON.parse(responseText);
                } catch (e) {
                    throw new Error('响应不是有效的JSON格式: ' + responseText);
                }
                
                if (response.ok && (responseData.success || responseData.code === 200)) {
                    result.className = 'result success';
                    result.textContent = '✅ API调用成功!\n\n' + 
                        '响应数据:\n' + JSON.stringify(responseData, null, 2);
                } else {
                    result.className = 'result error';
                    result.textContent = '❌ API调用失败!\n\n' + 
                        '状态码: ' + response.status + '\n' +
                        '错误信息: ' + (responseData.message || responseData.error || '未知错误') + '\n\n' +
                        '完整响应:\n' + JSON.stringify(responseData, null, 2);
                }
                
            } catch (error) {
                console.error('API调用失败:', error);
                result.className = 'result error';
                result.textContent = '❌ 网络错误或API不可用!\n\n' + 
                    '错误详情: ' + error.message + '\n\n' +
                    '可能的原因:\n' +
                    '1. 网络连接问题\n' +
                    '2. API服务器不可用\n' +
                    '3. CORS跨域问题\n' +
                    '4. API地址错误';
            } finally {
                button.disabled = false;
                button.textContent = '测试API调用';
            }
        }
        
        // 页面加载时显示说明
        window.onload = function() {
            console.log('API测试页面已加载');
            console.log('目标API:', 'https://test.yxyd.com/api/ask');
        };
    </script>
</body>
</html>
